library(tidyverse)
## ── Attaching packages ────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.1     ✓ purrr   0.3.4
## ✓ tibble  3.0.1     ✓ dplyr   1.0.0
## ✓ tidyr   1.1.0     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(dplyr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(DT)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gganimate)
library(transformr)
tsConfirmedLongGlobal <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")  %>% 
               pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                             names_to = "Date", values_to = "Confirmed") 
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## See spec(...) for full column specifications.
tsDeathsLongGlobal <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")) %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")  %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long),
               names_to = "Date", values_to = "Deaths")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## See spec(...) for full column specifications.
tsConfirmedLongGlobal <- tsConfirmedLongGlobal %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".", remove = FALSE)

tsDeathsLongGlobal <- tsDeathsLongGlobal %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".") %>% 
  select(Key, Deaths)

tsJoinedGlobal <- full_join(tsConfirmedLongGlobal, tsDeathsLongGlobal, by = c("Key")) %>% 
    select(-Key)

tsJoinedGlobal$Date <- mdy(tsJoinedGlobal$Date)

tsJoinedGlobalCounts <- tsJoinedGlobal %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long, Date),
               names_to = "Report_Type", values_to = "Counts")
pdf("exported_images/time_series_example_plot.pdf", width=6, height=3)
tsJoinedGlobal %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths")
dev.off()
## quartz_off_screen 
##                 2
ppi <- 300
png("exported_images/time_series_example_plot.png", width=6*ppi, height=6*ppi, res=ppi)
tsJoinedGlobal %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths")
dev.off()
## quartz_off_screen 
##                 2

US COVID-19 Deaths

US COVID-19 Deaths

ggplotly(
  tsJoinedGlobal %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
      geom_point() +
      geom_line() +
      ggtitle("US COVID-19 Deaths")
 )
US_deaths <- tsJoinedGlobal %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region == "US")
 p <- ggplot(data = US_deaths, aes(x = Date,  y = Deaths)) + 
        geom_point() +
        geom_line() +
        ggtitle("US COVID-19 Deaths")
ggplotly(p)
theme_set(theme_bw())
data_time <- tsJoinedGlobal %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region %in% c("China","Korea, South","Japan","Italy","US")) 
p <- ggplot(data_time, aes(x = Date,  y = Confirmed, color = Country_Region)) + 
      geom_point() +
      geom_line() +
      ggtitle("Confirmed COVID-19 Cases") +
      geom_point(aes(group = seq_along(Date))) +
      transition_reveal(Date) 
# animate(p,renderer = gifski_renderer(), end_pause = 15)
animate(p, end_pause = 15)

#Challenge 1

png("exported_images/time_series_Global_growth.png", width=3*ppi, height=3*ppi, res=ppi)
tsJoinedGlobal %>%
  group_by(Country_Region, Date) %>% 
  filter(Country_Region %in% c("US", "China", "Italy", "Brazil", "Japan", "Spain")) %>%
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    ggplot(aes(x = Date,  y = Confirmed, color =  Country_Region)) + 
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths") +
    facet_wrap(~Country_Region, ncol=2, scales="free_y")

dev.off()
## quartz_off_screen 
##                 2

US COVID-19 Deaths

#Challenge 2

temp <- tsJoinedGlobal %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region %in% c("China","Japan", "Korea, South", "Italy","Spain", "US")) %>% 
    ggplot(aes(x = Date,  y = Deaths)) + 
    geom_point() +
    geom_line() +
    ggtitle("COVID-19 Deaths") +
    facet_wrap(~Country_Region, ncol=2, scales="free_y")

ggplotly(temp)
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

#Challenge 3

tsConfirmedLongUS <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv")) %>%
  select(-c("UID", "iso2", "iso3", "code3",  "FIPS", "Admin2")) %>%
  pivot_longer(-c("Country_Region", "Lat", "Long_", "Combined_Key", "Province_State"), names_to = "Date", values_to = "Confirmed") %>%
  filter(Province_State != "Recovered")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   iso2 = col_character(),
##   iso3 = col_character(),
##   Admin2 = col_character(),
##   Province_State = col_character(),
##   Country_Region = col_character(),
##   Combined_Key = col_character()
## )
## See spec(...) for full column specifications.
topStates <- tsConfirmedLongUS %>%
  group_by(Province_State) %>%
  summarise(Confirmed = sum(Confirmed)) %>%
  arrange(desc(Confirmed)) %>%
  slice(1:10)
## `summarise()` ungrouping output (override with `.groups` argument)
topStates
## # A tibble: 10 x 2
##    Province_State Confirmed
##    <chr>              <dbl>
##  1 New York        67577546
##  2 California      60957581
##  3 Texas           51618828
##  4 Florida         51355166
##  5 New Jersey      28768548
##  6 Illinois        26806905
##  7 Georgia         23451163
##  8 Arizona         18002733
##  9 Massachusetts   17501126
## 10 Pennsylvania    16922645
tsConfirmedLongUS$Date <- mdy(tsConfirmedLongUS$Date)
mostCasesUS <- tsConfirmedLongUS %>%
  filter(Province_State %in% topStates$Province_State) %>%
  group_by(Date, Province_State) %>%
  summarise_at("Confirmed", sum) %>% 
  ggplot(aes(x = Date, y = Confirmed, color = Province_State)) +
  geom_point() +
  geom_line() +
  ggtitle("10 States with Highest Confirmed Rates") +
  geom_point(aes(group = seq_along(Date))) +
  transition_reveal(Date) 

animate(mostCasesUS, end_pause = 10)

Application written in R (R Core Team 2015) using the Shiny framework (Chang et al. 2015).

REFERENCES

Chang, W., J. Cheng, JJ. Allaire, Y. Xie, and J. McPherson. 2015. “Shiny: Web Application Framework for R. R Package Version 0.12.1.” Computer Program. http://CRAN.R-project.org/package=shiny.

R Core Team. 2015. “R: A Language and Environment for Statistical Computing.” Journal Article. http://www.R-project.org.